package com.ioc.business.base.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ioc.business.base.domain.StorageAreaLocation;
import com.ioc.business.base.domain.bo.storage.area.location.StorageAreaLocationBo;
import com.ioc.business.base.mapper.StorageAreaLocationMapper;
import com.ioc.business.base.service.IStorageAreaLocationService;
import com.ioc.business.base.service.IWarehouseUsageService;
import com.ioc.business.base.vo.storage.area.location.StorageAreaLocationItemVo;
import com.ioc.common.constant.UserConstants;
import com.ioc.common.exception.ServiceException;
import com.ioc.common.utils.CollectionUtils;
import com.ioc.common.utils.SecurityUtils;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 库区库位设置表 服务实现类
 * </p>
 *
 * @author WeiZhongRong
 * @since 2025-01-09
 */
@Service
@AllArgsConstructor
public class StorageAreaLocationServiceImpl extends ServiceImpl<StorageAreaLocationMapper, StorageAreaLocation> implements IStorageAreaLocationService {

    private final IWarehouseUsageService warehouseUsageService;

    @Override
    public List<StorageAreaLocationItemVo> getStorageAreaLocationList() {
        List<StorageAreaLocation> list = new LambdaQueryChainWrapper<>(baseMapper)
                .eq(StorageAreaLocation::getDelFlag, UserConstants.DEL_FLAG_NORMAL)
                .list();
        if (CollectionUtil.isEmpty(list)) {
            return new ArrayList<>(0);
        }
        return BeanUtil.copyToList(list, StorageAreaLocationItemVo.class);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean editStorageAreaLocation(List<StorageAreaLocationBo> boList) {
        List<String> storageAreaList = boList.stream().map(StorageAreaLocationBo::getStorageArea).collect(Collectors.toList());
        List<String> duplicates = CollectionUtils.findDuplicates(storageAreaList);
        if (CollectionUtil.isNotEmpty(duplicates)) {
            throw new ServiceException("库区【" + String.join(StrUtil.COMMA, duplicates) + "】存在重复！");
        }
        String username = SecurityUtils.getUsername(); // 获取当前用户名
        Date date = new Date(); // 获取当前日期

        List<StorageAreaLocation> storageAreaLocationList = new ArrayList<>(boList.size());
        List<StorageAreaLocationBo> updateList =
                boList.stream().filter(bo -> ObjectUtil.isNotEmpty(bo.getStorageAreaLocationId())).collect(Collectors.toList());
        if (CollectionUtil.isNotEmpty(updateList)) {
            List<StorageAreaLocation> storageAreaLocations = buildStorageAreaLocation(false, updateList, username, date);
            updateBatchById(storageAreaLocations);
            storageAreaLocationList.addAll(storageAreaLocations);
        }
        List<StorageAreaLocationBo> saveList =
                boList.stream().filter(bo -> ObjectUtil.isEmpty(bo.getStorageAreaLocationId())).collect(Collectors.toList());
        if (CollectionUtil.isNotEmpty(saveList)) {
            List<StorageAreaLocation> storageAreaLocations = buildStorageAreaLocation(true, saveList, username, date);
            saveBatch(storageAreaLocations);
            storageAreaLocationList.addAll(storageAreaLocations);
        }

        if (CollectionUtil.isNotEmpty(storageAreaLocationList)) {
            warehouseUsageService.editWarehouseUsage(storageAreaLocationList);
        }
        return false;
    }

    private List<StorageAreaLocation> buildStorageAreaLocation(boolean save, List<StorageAreaLocationBo> list, String username, Date date) {
        return list.stream().map(i -> {
            StorageAreaLocation storageAreaLocation = new StorageAreaLocation();
            BeanUtils.copyProperties(i, storageAreaLocation);
            if (save) {
                storageAreaLocation.setCreateBy(username);
                storageAreaLocation.setCreateTime(date);
            }
            storageAreaLocation.setUpdateBy(username);
            storageAreaLocation.setCreateTime(date);
            return storageAreaLocation;
        }).collect(Collectors.toList());
    }
}
